package com.xy.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import com.xy.constants.XyAppConstant;
import com.xy.properties.XxjobProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.launch.utils.INetUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * xxl-job config
 *
 * @author xuxueli 2017-04-28
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
@AllArgsConstructor
public class XxlJobConfig {
	private final Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

	private final XxjobProperties properties;
//	private final InetUtils inetUtils;

	private Environment environment;


	@Bean
	public XxlJobSpringExecutor xxlJobExecutor() {
		logger.info(">>>>>>>>>>> xxl-job config init.");
		XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
		xxlJobSpringExecutor.setAdminAddresses(properties.getAddresses());
		xxlJobSpringExecutor.setAppName(XyAppConstant.APPLICATION_SCREEN);
		String property = environment.getProperty("server.port");
		xxlJobSpringExecutor.setIp(INetUtil.getHostIp());
		xxlJobSpringExecutor.setPort(Func.toInt(property)+10);
		xxlJobSpringExecutor.setAccessToken(properties.getAccessToken());
		xxlJobSpringExecutor.setLogPath(properties.getLogPath());
		xxlJobSpringExecutor.setLogRetentionDays(properties.getLogRetentionDays());
//		String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
		log.info("Ip{}：",INetUtil.getHostIp());
		return xxlJobSpringExecutor;
	}

	/**
	 * 针对多网卡、容器内部署等情况，可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP；
	 *
	 *      1、引入依赖：
	 *          <dependency>
	 *             <groupId>org.springframework.cloud</groupId>
	 *             <artifactId>spring-cloud-commons</artifactId>
	 *             <version>${version}</version>
	 *         </dependency>
	 *
	 *      2、配置文件，或者容器启动变量
	 *          spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
	 *
	 *      3、获取IP
	 *          String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
	 */


}
